package cc.woori.mobile;

import java.io.IOException;
import java.util.Iterator;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cc.woori.mobile.bean.PodCastEntity;

public class RegisterPodCastServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 2689351737904927401L;
    public final static String POD_CAST_PROP_PATH = "./prop_potcast.properties";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	    throws ServletException, IOException {

	String title = req.getParameter("title");
	String subtitle = req.getParameter("subtitle");
	String web_url = req.getParameter("web_url");
	String author = req.getParameter("author");
	String summary = req.getParameter("summary");
	String description = req.getParameter("description");
	String owner = req.getParameter("owner");
	String owner_email = req.getParameter("owner_email");
	String img_url = req.getParameter("img_url");

	PersistenceManager pm = PMF.get().getPersistenceManager();
	PodCastEntity pce = null;

	try {
	    Extent<PodCastEntity> ext = pm.getExtent(PodCastEntity.class);
	    Iterator<PodCastEntity> iterator = ext.iterator();
	    if (iterator.hasNext()) {
		pce = iterator.next();
		pce.setTitle(title);
		pce.setSubtitle(subtitle);
		pce.setLink(web_url);
		pce.setAuthor(author);
		pce.setSummary(summary);
		pce.setDescription(description);
		pce.setOwner(owner);
		pce.setOwner_email(owner_email);
		pce.setImg_url(img_url);
	    } else {
		pce = new PodCastEntity(title, subtitle, web_url, author,
			summary, description, owner, owner_email, img_url);
	    }
	    pm.makePersistent(pce);
	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    pm.close();
	}
    }
}